<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>Vue-router笔记 |  欢迎</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="欢迎" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Vue学习/vueRouter"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Vue-router笔记
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2022/01/19/Vue%E5%AD%A6%E4%B9%A0/vueRouter/" class="article-date">
  <time datetime="2022-01-19T04:26:12.000Z" itemprop="datePublished">2022-01-19</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Vue%E5%AD%A6%E4%B9%A0/">Vue学习</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.7k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">12 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h3><hr>
<ul>
<li><p>导入vue-router插件</p>
</li>
<li><p>创建<router-link>标签，默认渲染成a标签</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">&#x27;/foo&#x27;</span>&gt;</span>GO TO FOO<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>创建路由出口</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">router-view</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span>		</span><br></pre></td></tr></table></figure></li>
<li><p>配置路由并使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义component</span></span><br><span class="line"><span class="keyword">const</span> Foo = &#123; <span class="attr">template</span>: <span class="string">&#x27;&lt;p&gt;THIS IS FOO&lt;/p&gt;&#x27;</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> Bar = &#123; <span class="attr">template</span>: <span class="string">&#x27;&lt;p&gt;THIS IS BAR&lt;/p&gt;&#x27;</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义路由映射表</span></span><br><span class="line"><span class="keyword">const</span> routes = [&#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/foo&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: Foo,</span><br><span class="line">&#125;, &#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/bar&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: Bar,</span><br><span class="line">&#125;]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建路由实例</span></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>: routes,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&quot;#app&quot;</span>,</span><br><span class="line">    <span class="attr">router</span>: router,	<span class="comment">//注册路由</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="动态路由匹配"><a href="#动态路由匹配" class="headerlink" title="动态路由匹配"></a>动态路由匹配</h3><hr>
<blockquote>
<p><strong>组件中，可以使用 <code>this.route.params.x</code> 来获取参数</strong></p>
</blockquote>
<p>动态绑定路由的方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> routes = [&#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/foo/:id&#x27;</span>,	<span class="comment">//:参数名即可（与vue中动态绑定属性一致）</span></span><br><span class="line">    <span class="attr">component</span>: Foo,</span><br><span class="line">&#125;, &#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/bar&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: Bar,</span><br><span class="line">&#125;];</span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&quot;#app&quot;</span>,</span><br><span class="line">    <span class="attr">router</span>: router,	<span class="comment">//注册路由</span></span><br><span class="line">    <span class="attr">computed</span>:&#123;</span><br><span class="line">        <span class="function"><span class="title">ID</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.$route.params.id;	<span class="comment">//可以直接获取动态绑定参数</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="通配符路径"><a href="#通配符路径" class="headerlink" title="通配符路径"></a>通配符路径</h3><hr>
<blockquote>
<p>使用 <code>*</code> 来表示任意字符串</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> routes = [&#123;</span><br><span class="line">    <span class="attr">path</span>:<span class="string">&#x27;*&#x27;</span>,	<span class="comment">//匹配所有路径</span></span><br><span class="line">&#125;,&#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/foo/:id&#x27;</span>,	<span class="comment">//:参数名即可（与vue中动态绑定属性一致）</span></span><br><span class="line">    <span class="attr">component</span>: Foo,</span><br><span class="line">&#125;, &#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/bar&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: Bar,</span><br><span class="line">&#125;];</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用    <code>this.$route.params.pathMatch</code>     获取通配符表示的字符串</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> routes = [&#123;</span><br><span class="line">    <span class="attr">path</span>:<span class="string">&#x27;*&#x27;</span>,	<span class="comment">//匹配所有路径</span></span><br><span class="line">&#125;,&#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/user-*&#x27;</span>,	<span class="comment">//:参数名即可（与vue中动态绑定属性一致）</span></span><br><span class="line">&#125;];</span><br><span class="line"><span class="comment">// 当访问路径问/user-admin时</span></span><br><span class="line"><span class="built_in">this</span>.$route.params.pathMatch;	<span class="comment">//&#x27;admin&#x27;</span></span><br><span class="line"><span class="comment">// 当访问路径问/non-existing时</span></span><br><span class="line"><span class="built_in">this</span>.$route.params.pathMatch;	<span class="comment">//&#x27;non-existing&#x27;</span></span><br></pre></td></tr></table></figure>



<h3 id="匹配优先级"><a href="#匹配优先级" class="headerlink" title="匹配优先级"></a>匹配优先级</h3><hr>
<p>同一个路径，可匹配多个路由。路由定义越早，优先级越高。</p>
<h3 id="嵌套路由"><a href="#嵌套路由" class="headerlink" title="嵌套路由"></a>嵌套路由</h3><hr>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">/user/foo/profile                     /user/foo/posts</span><br><span class="line">+------------------+                  +-----------------+</span><br><span class="line">| User             |                  | User            |</span><br><span class="line">| +--------------+ |                  | +-------------+ |</span><br><span class="line">| | Profile      | |  +------------&gt;  | | Posts       | |</span><br><span class="line">| |              | |                  | |             | |</span><br><span class="line">| +--------------+ |                  | +-------------+ |</span><br><span class="line">+------------------+                  +-----------------+</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义component</span></span><br><span class="line"><span class="keyword">const</span> Foo = &#123; <span class="attr">template</span>: <span class="string">`&lt;div&gt;&lt;p&gt;THIS IS FOO&lt;/p&gt;&lt;router-view&gt;&lt;/router-view&gt;&lt;/div&gt;`</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义路由映射表</span></span><br><span class="line"><span class="keyword">const</span> routes = [&#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/foo&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: Foo,</span><br><span class="line">    <span class="attr">children</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;profile&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:FooProfile,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;posts&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:FooPosts,</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建路由实例</span></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>: routes,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&quot;#app&quot;</span>,</span><br><span class="line">    <span class="attr">router</span>: router,	<span class="comment">//注册路由</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="编程式导航"><a href="#编程式导航" class="headerlink" title="编程式导航"></a>编程式导航</h3><hr>
<ul>
<li>this.$router.push(xx)    如同调用<router-link to='xx'></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串</span></span><br><span class="line"><span class="built_in">this</span>.$router.push(<span class="string">&#x27;home&#x27;</span>);		<span class="comment">// /home</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象</span></span><br><span class="line"><span class="built_in">this</span>.$router.push(&#123;<span class="attr">path</span>:<span class="string">&#x27;home&#x27;</span>&#125;);		<span class="comment">// /home</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 命名的路由</span></span><br><span class="line"><span class="built_in">this</span>.$router.push(&#123;<span class="attr">name</span>:<span class="string">&#x27;user&#x27;</span>,<span class="attr">params</span>:&#123;<span class="attr">userId</span>:<span class="string">&#x27;123&#x27;</span>&#125;&#125;);		<span class="comment">// /user/123	= /user/:userId</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 带查询参数</span></span><br><span class="line"><span class="built_in">this</span>.$router.push(&#123;<span class="attr">path</span>:<span class="string">&#x27;login&#x27;</span>,<span class="attr">query</span>:&#123;username：<span class="string">&#x27;123&#x27;</span>&#125;&#125;);	<span class="comment">// /login?username=123</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>当对象中，具有path的时候，params会失效</p>
</blockquote>
<ul>
<li>this.$router.replace(xx)    如同调用<router-link to='xx' replace></li>
</ul>
<blockquote>
<p>特点在于：不会像history添加新记录，会替换当前记录。</p>
</blockquote>
<blockquote>
<p>传参方式与push一致</p>
</blockquote>
<ul>
<li>this.$router.go(n)</li>
</ul>
<blockquote>
<p>参数是整数，表示在history中向前、退后多少步。用正负表示前后</p>
</blockquote>
<blockquote>
<p>不管是repalce、go、push在history、hash、abstract模式下的表现都是一致的</p>
</blockquote>
<h3 id="命名路由"><a href="#命名路由" class="headerlink" title="命名路由"></a>命名路由</h3><hr>
<ul>
<li>用于标识一个路由</li>
<li>可以通过名字，寻找到路由<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建具名路由</span></span><br><span class="line">    <span class="keyword">const</span> routes = [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/user/:userId&#x27;</span>,</span><br><span class="line">            <span class="attr">name</span>:<span class="string">&#x27;user&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:User,</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line"><span class="comment">// 调用具名路由</span></span><br><span class="line">router.push(&#123;<span class="attr">name</span>:<span class="string">&#x27;user&#x27;</span>,<span class="attr">params</span>:&#123;<span class="attr">userId</span>:<span class="number">123</span>&#125;&#125;); <span class="comment">// 访问/user/123</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="命名视图"><a href="#命名视图" class="headerlink" title="命名视图"></a>命名视图</h3><hr>
<p>使得不同视图，准确地响应不同的组件。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">router-view</span>&gt;</span>默认视图<span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">&#x27;aView&#x27;</span>&gt;</span>aView视图<span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">&#x27;bView&#x27;</span>&gt;</span>bView视图<span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> vueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">            <span class="attr">components</span>:&#123;</span><br><span class="line">                <span class="attr">default</span>:Head,</span><br><span class="line">                <span class="attr">aView</span>:aComponent, </span><br><span class="line">                <span class="attr">bView</span>:bComponent,</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="重定向"><a href="#重定向" class="headerlink" title="重定向"></a>重定向</h3><hr>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> vueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">            redirect：<span class="string">&#x27;/index&#x27;</span>, <span class="comment">//重定向到主页</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/error&#x27;</span>,</span><br><span class="line">            <span class="attr">name</span>:<span class="string">&#x27;error&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:errorComponent,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;*&#x27;</span></span><br><span class="line">            <span class="attr">redirect</span>:&#123;<span class="attr">name</span>:<span class="string">&#x27;error&#x27;</span>&#125;   <span class="comment">//重定向到error路由</span></span><br><span class="line">        &#125;,</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="别名-alias"><a href="#别名-alias" class="headerlink" title="别名 alias"></a>别名 alias</h3><hr>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 不管访问 /a 还是 /b 都是按照user这个路由来渲染A组件</span></span><br><span class="line"><span class="attr">routes</span>:[</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="attr">name</span>:<span class="string">&#x27;user&#x27;</span></span><br><span class="line">        <span class="attr">path</span>:<span class="string">&#x27;/a&#x27;</span>,</span><br><span class="line">        <span class="attr">alias</span>:<span class="string">&#x27;/b&#x27;</span></span><br><span class="line">        <span class="attr">component</span>:A,</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="路由组件传参"><a href="#路由组件传参" class="headerlink" title="路由组件传参"></a>路由组件传参</h3><hr>
<p>使用$route.params.xx 会增加耦合度</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> User = &#123;</span><br><span class="line">    <span class="attr">template</span>:<span class="string">&#x27;&lt;div&gt;&#123;&#123; $route.params.id &#125;&#125;&lt;/div&gt;&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/user/:id&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:User,</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>使用props解耦</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> User = &#123;</span><br><span class="line">    <span class="attr">template</span>: <span class="string">&#x27;&lt;div&gt;&#123;&#123; id &#125;&#125;&lt;/div&gt;&#x27;</span>,</span><br><span class="line">    <span class="attr">props</span>: [<span class="string">&#x27;id&#x27;</span>],</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> Name = &#123;</span><br><span class="line">    <span class="attr">template</span>: <span class="string">&#x27;&lt;div&gt;&#123;&#123; name &#125;&#125;&lt;/div&gt;&#x27;</span>,</span><br><span class="line">    <span class="attr">props</span>: [<span class="string">&#x27;name&#x27;</span>],</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>: [</span><br><span class="line">        <span class="comment">// 单个视图写法：</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>: <span class="string">&#x27;/user/:id&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>: User,</span><br><span class="line">            <span class="attr">props</span>: <span class="literal">true</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 多个视图写法：</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>: <span class="string">&#x27;/person/:id&#x27;</span>,</span><br><span class="line">            <span class="attr">components</span>: &#123;</span><br><span class="line">                <span class="attr">default</span>: User, <span class="comment">// 视图名：组件名</span></span><br><span class="line">                <span class="attr">showName</span>: Name</span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="attr">props</span>: &#123;</span><br><span class="line">                <span class="attr">default</span>: <span class="literal">true</span>, <span class="comment">//表示该视图，启用props传参</span></span><br><span class="line">                <span class="attr">showNmae</span>: <span class="literal">true</span>,</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="HTML5-History模式"><a href="#HTML5-History模式" class="headerlink" title="HTML5 History模式"></a>HTML5 History模式</h3><hr>
<p><code>vue-router</code>默认采用hash模式：</p>
<ul>
<li>hash：当URL改变时，页面不会重新加载</li>
<li>history：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">mode</span>:<span class="string">&#x27;histroy&#x27;</span>,</span><br><span class="line">    <span class="attr">routes</span>:[],</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="进阶内容："><a href="#进阶内容：" class="headerlink" title="进阶内容："></a>进阶内容：</h2><hr>
<h3 id="导航守卫"><a href="#导航守卫" class="headerlink" title="导航守卫"></a>导航守卫</h3><blockquote>
<p>导航守卫：个人理解为：对路由的跳转进行操作</p>
</blockquote>
<blockquote>
<p>导航解析流程：</p>
</blockquote>
<ul>
<li>触发导航</li>
<li>在<strong>失活组件</strong>中,调用<code>beforeRouteLeave</code>守卫；</li>
<li>调用全局的<code>beforeEach</code>守卫；</li>
<li>在<strong>重用组件</strong>中,调用<code>beforeUpdate</code>守卫；</li>
<li>在<strong>路由配置</strong>中，调用<code>beforeEnter</code>守卫；</li>
<li>解析异步路由组件；</li>
<li>在<strong>被激活组件</strong>中，调用<code>beforeRouteEnter</code>守卫；</li>
<li>调用全局的<code>beforeResolve</code>守卫</li>
<li>导航被确认</li>
<li>调用全局的<code>afterEach</code>钩子</li>
<li>触发DOM更新</li>
<li>调用<code>beforeRouteEnter</code>守卫中传给<code>next</code>的回调函数。<blockquote>
<p>分类：</p>
</blockquote>
</li>
<li>全局前置守卫：<blockquote>
<p>守卫是异步执行的，此时导航在所有守卫resolve完之前，一直处于等待状态</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>每次路由跳转前都会调用</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;....&#125;);</span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * to：即将进入的目标路由对象</span></span><br><span class="line"><span class="comment">        * from：当前正要离开的路由对象</span></span><br><span class="line"><span class="comment">        * next：是一个方法，用来resolve这个钩子</span></span><br><span class="line"><span class="comment">            - next()        执行下一个钩子</span></span><br><span class="line"><span class="comment">            - next(false)   中断当前导航，如果没有对url进行操作，则会重置到from路由的地址</span></span><br><span class="line"><span class="comment">            - next(&#x27;url&#x27;) 或 next(&#123;path:&#x27;url&#x27;&#125;)     中断当前导航，跳转到新的导航</span></span><br><span class="line"><span class="comment">            - next(error)   中断当前导航，当前错误会被传递给router.onError()注册过的回调</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="comment">// 判断是否登录成功</span></span><br><span class="line">    router.beforeEach(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(to.isLogin == <span class="literal">true</span> )&#123;</span><br><span class="line">            next(); <span class="comment">//如果登录成功，执行下一个导航</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            next(<span class="string">&#x27;login&#x27;</span>);  <span class="comment">//不然就返回登录页面</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>全局解析守卫：<blockquote>
<p>使用<code>router.beforeResolve</code>注册全局守卫，与<code>router.beforeEach</code>类似<br>区别：导航被确认前，同时在所有组件内守卫和异步路由被解析之后，解析守卫就被调用。</p>
</blockquote>
</li>
<li>全局后置钩子：<blockquote>
<p>与守卫不一样，钩子不会接收next，所以不能改变导航本身。</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>每次路由跳转后，组件响应前都会调用</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">router.afterEach(<span class="function">(<span class="params">to,<span class="keyword">from</span></span>)=&gt;</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<ul>
<li>路由独享守卫：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">path</span>:<span class="string">&#x27;/foo&#x27;</span>,</span><br><span class="line">            <span class="attr">component</span>:Foo,</span><br><span class="line">            <span class="attr">beforeEnter</span>:<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line"></span><br><span class="line">            &#125;,</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li>组件内的守卫：<ul>
<li>beforeRouteEnter();</li>
<li>beforeRouteUpdate();</li>
<li>beforeRouteLeave();<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Foo=&#123;</span><br><span class="line">    <span class="attr">template</span>:<span class="string">`...`</span>,</span><br><span class="line">    beforeRouteEnter(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="comment">// 在渲染该组件的对应路由被comfirm之前调用</span></span><br><span class="line">        <span class="comment">// 不能获取this实例，因为守卫执行前，组件实例还没有被创建</span></span><br><span class="line">        <span class="comment">// 但是可以通过如下方法获取this实例</span></span><br><span class="line">        next(<span class="function"><span class="params">vm</span> =&gt;</span>&#123;</span><br><span class="line">            <span class="comment">// 通过vm访问组件实例</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line">    beforeRouteUpdate(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="comment">// 在当前路由改变，组件被复用时调用</span></span><br><span class="line">        <span class="comment">// 比如说，在/foo/:id的路由中，从/foo/1到/foo/2时，会调用</span></span><br><span class="line">        <span class="comment">// 可以获取this实例，因为在/foo/1中，组件已经被创建</span></span><br><span class="line">    &#125;);</span><br><span class="line">    beforeRouteLeave(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="comment">// 在导航离开该组件的对应路由时调用</span></span><br><span class="line">        <span class="comment">// 可以获取this实例</span></span><br><span class="line">        <span class="comment">// 通常用于 禁止用户未保存而突然离开</span></span><br><span class="line">        <span class="keyword">const</span> answer = <span class="built_in">window</span>.confirm(<span class="string">&#x27;Do you realy want to leave? you have unsaved changes!&#x27;</span>);</span><br><span class="line">        <span class="keyword">if</span>(anser)&#123;</span><br><span class="line">            next();</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            next(<span class="literal">false</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<hr>
<h3 id="路由元信息"><a href="#路由元信息" class="headerlink" title="路由元信息"></a>路由元信息</h3><hr>
<blockquote>
<p>使路由写带某些信息，同时可以在钩子、守卫中获取</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[&#123;</span><br><span class="line">        <span class="attr">path</span>:<span class="string">&#x27;/about&#x27;</span>,</span><br><span class="line">        <span class="attr">component</span>:About,</span><br><span class="line">        <span class="attr">meta</span>:&#123;</span><br><span class="line">            <span class="attr">title</span>:<span class="string">&quot;this is about&quot;</span>,</span><br><span class="line">            <span class="attr">flag</span>:<span class="number">0</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,&#123;</span><br><span class="line">        <span class="attr">path</span>:<span class="string">&#x27;/index&#x27;</span>，</span><br><span class="line">        <span class="attr">component</span>:Index,</span><br><span class="line">        <span class="attr">meta</span>:&#123;</span><br><span class="line">            <span class="attr">title</span>:<span class="string">&quot;this is index&quot;</span>,</span><br><span class="line">            <span class="attr">message</span>:<span class="string">&quot;welcome to the index&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;]</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在钩子、守卫中调用路由元信息</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> About = &#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params">h</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> h(<span class="string">&#x27;p&#x27;</span>,<span class="string">&quot;this is about component&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 调用路由守卫</span></span><br><span class="line">    beforeRouteEnter(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        ert(<span class="string">&quot;从路由&quot;</span>+<span class="keyword">from</span>.meta.title+<span class="string">&quot;中过来的&quot;</span>);</span><br><span class="line">        next();</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> Index = &#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params">h</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> h(<span class="string">&#x27;p&#x27;</span>,<span class="string">&quot;this is index component&quot;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    beforeRouteEnter(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</span><br><span class="line">        alert(<span class="string">&quot;从路由&quot;</span>+<span class="keyword">from</span>.meta.title+<span class="string">&quot;中过来的&quot;</span>);</span><br><span class="line">        next();</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="数据获取"><a href="#数据获取" class="headerlink" title="数据获取"></a>数据获取</h3><hr>
<blockquote>
<p>数据的获取可以分成以下两种流程</p>
</blockquote>
<ol>
<li>完成导航之后获取；先完成导航，然后在组件的生命周期钩子中获取数据。在获取数据期间，显示“加载中”之类的提示</li>
<li>完成导航之前获取；在导航完成之前，在路由进入守卫中获取数据，获取成功后再进行导航。<blockquote>
<p>按照需求来选择</p>
</blockquote>
</li>
</ol>
<hr>
<h3 id="滚动行为"><a href="#滚动行为" class="headerlink" title="滚动行为"></a>滚动行为</h3><hr>
<blockquote>
<p>当切换到新路由时，想要页面滚到顶部或是保持原先滚动位置。可以使用<code>scrollBehavior</code>实现</p>
</blockquote>
<blockquote>
<p>仅适用于支持<code>history.pashState</code>的浏览器。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    <span class="attr">routes</span>:[],</span><br><span class="line">    <span class="function"><span class="title">scrollBehavior</span>(<span class="params">to,<span class="keyword">from</span>,savedPosition</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// return 期望滚到的位置</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        &#123;x:number,y:number&#125;</span></span><br><span class="line"><span class="comment">        &#123;selector:string,offset&#125;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        <span class="comment">// 滚动到顶端</span></span><br><span class="line">        <span class="keyword">if</span>(to.meta.MessageString == <span class="string">&#x27;滚动到顶端&#x27;</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> &#123;<span class="attr">x</span>:<span class="number">0</span>,<span class="attr">y</span>:<span class="number">0</span>&#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(to.meta.MessageString == <span class="string">&#x27;滚动到锚点&#x27;</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                <span class="attr">selector</span>:<span class="string">&#x27;#app&#x27;</span>,   <span class="comment">// 选择器(id选择器)来选择锚点进行滑动</span></span><br><span class="line">                <span class="attr">behavior</span>:<span class="string">&#x27;smooth&#x27;</span>   <span class="comment">// 进行平滑滑动</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="路由懒加载"><a href="#路由懒加载" class="headerlink" title="路由懒加载"></a>路由懒加载</h3><hr>
<blockquote>
<p>懒加载？可以叫做按需加载，当该路由被访问的时候，才加载对应的组件，从而提高效率</p>
</blockquote>
<blockquote>
<p>一般是结合<code>Vue异步组件</code>和<code>webpack代码分割</code>,实现路由组件的懒加载。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Foo = <span class="function">() =&gt;</span> <span class="keyword">import</span>(./src/components/Foo.vue<span class="string">&#x27;);   //异步加载</span></span><br><span class="line"><span class="string">const router = new VueRouter(&#123;</span></span><br><span class="line"><span class="string">    routes:[&#123;</span></span><br><span class="line"><span class="string">        path:&#x27;</span>/foo<span class="string">&#x27;,</span></span><br><span class="line"><span class="string">        component:Foo</span></span><br><span class="line"><span class="string">    &#125;]</span></span><br><span class="line"><span class="string">&#125;)</span></span><br><span class="line"><span class="string"></span></span><br></pre></td></tr></table></figure>

<hr>
<h3 id="导航故障"><a href="#导航故障" class="headerlink" title="导航故障"></a>导航故障</h3><hr>
<blockquote>
<p>表示一次导航失败。</p>
</blockquote>
<blockquote>
<p>导航原理：<br>当使用<code>&lt;router-link&gt;</code>的时候，会调用<code>router.push</code>来触发一次导航。<br>但是如下问题会使得导航失败：</p>
</blockquote>
<ol>
<li>用于已经位于目标页面;</li>
<li>导航守卫调用了:<code>next(false)</code>;</li>
<li>导航守卫抛出错误，或调用<code>next(new Error())</code>;</li>
</ol>
<blockquote>
<p>如何检测故障是否来自路由?<br>使用<code>isNavigationFailure</code>函数可以检测。</p>
</blockquote>
<blockquote>
<p>如何区分导航故障类型？<br>使用<code>NavigationFailureType</code>可以区分不同类型的导航故障：</p>
</blockquote>
<ol>
<li>redirected： 导航守卫中调用了<code>next(newLocation)</code>,重定向到其他地方了</li>
<li>aborted：    导航守卫调用了<code>next(false)</code>,中断了本次导航</li>
<li>cancelled:   导航过程被取消了，转移到新的导航上。</li>
<li>duplicated:  导航被阻止，因为用户已经在目标位置了。</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入方法和对象</span></span><br><span class="line"><span class="keyword">import</span> &#123; isNavigationFailure,NavigationFailureType &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-router&#x27;</span>;</span><br><span class="line"><span class="comment">// 或者</span></span><br><span class="line"><span class="keyword">import</span> VueRouter <span class="keyword">from</span> <span class="string">&#x27;vue-router&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> &#123; isNavigationFailure,NavigationFailureType &#125;  = VueRouter;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">router.push(<span class="string">&#x27;/admin&#x27;</span>).catch(<span class="function"><span class="params">failure</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(isNavigationsFailure(failure,NavigationFailureType.redirected))&#123;</span><br><span class="line">        alert(<span class="string">&quot;已经重定向到其他地方了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2022/01/19/Vue%E5%AD%A6%E4%B9%A0/vueRouter/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Vue-js/" rel="tag">Vue.js</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2022/01/19/Vue%E5%AD%A6%E4%B9%A0/vuex/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Vuex笔记
          
        </div>
      </a>
    
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "jeb2pvxjdeJQOj5OG3AUavGv-gzGzoHsz",
    app_key: "6YlB9tQJTstHMM8GOwFu4uyY",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2022
        <i class="ri-heart-fill heart_icon"></i> 子与子乘
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="欢迎"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i></p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>